<template>
  <div class="swiper">
    <el-breadcrumb separator="/" class="mianbao">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图列表</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="flex">
      <el-button type="primary" @click="toswiperadd()">
        添加轮播图 &nbsp;<el-icon><Plus /></el-icon>
      </el-button>
    </div>

     <el-table v-loading='loading' :data="tableData" border stripe style="width: 100%">
    <el-table-column label="序号" type='index' width="80"/>
     <el-table-column label="图片" >
        <template #default="scope">
          <img :src="scope.row.img" alt="">
        </template>
     </el-table-column>
      <el-table-column label="图片链接" >
           <template #default="scope">
          <el-link type="primary" :href="scope.row.link">链接</el-link>
        </template>
      </el-table-column>
       <el-table-column label="提示" prop='alt'/>
    <el-table-column label="是否展示">
         <template #default="scope">
           <el-switch v-model="scope.row.flag" @click="zhuangtai(scope.row.bannerid,scope.row.flag)"/>
        </template>
    </el-table-column>
    <el-table-column label="操作" >
        <template #default="scope">
            <el-button @click="dalatetu(scope.row.bannerid)" type="danger">删除</el-button>
        </template>
    </el-table-column>
  </el-table>
  </div>
</template>

<script setup>
import {ref,onMounted} from 'vue'
import{bannerlist,bannerdelete,updateFlag} from '@/api/index'
import{useRouter} from 'vue-router'
import { ElNotification } from 'element-plus'
import 'element-plus/es/components/notification/style/css'

var tableData=ref([])
var $router=useRouter()
var loading=ref(false)

onMounted(async()=>{
  loading.value=true
  var ok= await bannerlist()
  console.log(ok);
  tableData.value=ok.data.data
  loading.value=false
})
var dalatetu= async (bannerid)=>{
    var ok= await bannerdelete({bannerid})
    console.log(ok);
    if(ok.data.code==200){
       ElNotification({
          title:'删除通知',
          message:ok.data.message,
          type:'success',
          duration: 1500
        })
      var i=tableData.value.findIndex(item=>item.bannerid==bannerid)
      if(i!=-1) tableData.value.splice(i,1)
    }
}
var toswiperadd=()=>{
  $router.push('/index/swiperadd')
}

var zhuangtai= async (a,b)=>{
  var ok=await updateFlag({a,b})
  console.log(ok);
  if(ok.data.code==200){
      ElNotification({
          title:'图片状态通知',
          message:'状态更改成功',
          type:'success',
          duration: 1500
        })
  }
}
</script>

<style scoped lang='scss'>
.flex{
  margin: 20px 0;
}
.swiper{
  img{
    width: 80%;
    height: 50px;
    object-fit: cover;
  }
}
</style>